//
//  YRStatusPictureView.swift
//  YR 微博
//
//  Created by cyr on 15/10/18.
//  Copyright © 2015年 cyr. All rights reserved.
//

import UIKit
import SDWebImage

private let statusPictureViewCellID = "statusPictureViewCellID"

class YRStatusPictureView: UICollectionView {

    var status: YRStatus? {
        didSet {
            sizeToFit()
            
            // 刷新数据
            reloadData()
        }
    }
    
    override func sizeThatFits(size: CGSize) -> CGSize {
        return calcViewSize()
    }
    
    /**
    *  计算视图的大小
    */
    private func calcViewSize() -> CGSize {
        let itemSize = CGSize(width: 90, height: 90)
        let rowCount = 3
        let margin: CGFloat = 10
        
        pictureLayout.itemSize = itemSize
        
        // 根据图片梳理计算视图大小
        let count = status?.pictureURLs?.count ?? 0
        
        // 1.没有图片
        if count == 0 {
            return CGSizeZero
        }
        // 2.1张图片
        if count == 1 {
            let key = status!.pictureURLs![0].absoluteString
            
            let image = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(key)
            
            var size = CGSize(width: 150, height: 120)
            if image != nil {
                size = image.size
            }
            
            // 对图像尺寸做进一步判断
            size.width < 40 ? 40 : size.width
            size.width > UIScreen.mainScreen().bounds.size.width ? 150 : size.width
            
            
            pictureLayout.itemSize = size
            return size
        }
        
        // 3.4张图片
        if count == 4 {
            let w = itemSize.width * 2 + margin
            return CGSize(width: w, height: w)
        }
        
        // 4. 其他
        let row = (count - 1) / rowCount + 1
        let w = itemSize.width * CGFloat(rowCount) + margin * CGFloat(rowCount - 1)
        let h = itemSize.height * CGFloat(row) + margin * CGFloat(row - 1)
        
        return CGSize(width: w, height: h)
        
        
    }


    /**
    *  图片布局
    */
    private let pictureLayout = UICollectionViewFlowLayout()
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: pictureLayout)
        
        backgroundColor = UIColor.lightGrayColor()
        
        // 注册可以重用的 cell
        registerClass(YRStatusPictureViewCell.self, forCellWithReuseIdentifier: "statusPictureViewCellID")
        
        // 设置数据源
        self.dataSource = self
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}

extension YRStatusPictureView: UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return status?.pictureURLs?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("statusPictureViewCellID", forIndexPath: indexPath) as! YRStatusPictureViewCell
      
        cell.imageURL = status!.pictureURLs![indexPath.item]
        
        return cell
    }
}


class  YRStatusPictureViewCell: UICollectionViewCell {
    
    var imageURL: NSURL? {
        didSet {
            iconView.sd_setImageWithURL(imageURL!)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /**
    设置界面
    */
    private func setupUI() {
        
        addSubview(iconView)
        
        iconView.ff_Fill(contentView)
        
    }
    
    
    // MARK: - 懒加载
    private lazy var iconView: UIImageView = {
    
        let iv = UIImageView()
        // 等比例缩放
        iv.contentMode = UIViewContentMode.ScaleAspectFill
        
        // 超出边界范围的裁剪掉
        iv.clipsToBounds = true
        
        return iv
        
    }()
    
}
